package org.springblade.modules.admin.controller.suger;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.sugar.rescue.SugarRescueSearchDTO;
import org.springblade.modules.admin.service.BuildingService;
import org.springblade.modules.admin.service.IMaintainMalfunctionRescueService;
import org.springblade.modules.admin.service.IMaintainPlanContentExtService;
import org.springblade.modules.admin.vo.BuildingRescueScreenVO;
import org.springblade.modules.admin.vo.ElevatorRescueScreenVO;
import org.springblade.modules.admin.vo.UserRescueScreenVO;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 救援大屏各种分组开工Controller
 * @author Sharry
 * @since 2023/9/18
 */
@RestController
@RequestMapping("/admin/rescue/groups/screen")
@AllArgsConstructor
@Api( tags = "救援大屏-条件统计",value = "/admin/rescue/groups/screen")
public class RescueScreenController {

	private final BuildingService buildingService;

	private final IMaintainPlanContentExtService maintainPlanContentExtService;

	private final IMaintainMalfunctionRescueService rescueService;

	@GetMapping("/init")
	@ApiOperation("初始化数据预留接口,补充到达延迟相关信息")
	@ApiOperationSupport(order = 10)
	public R<Boolean> initArrive(){
		return R.data(maintainPlanContentExtService.init());
	}

	@PostMapping("/project")
	@ApiOperationSupport(order = 31)
	@ApiOperation("困人救援高发项目top10")
	public R<List<BuildingRescueScreenVO>> byBuilding(@RequestBody SugarRescueSearchDTO searchDTO){
		List<BuildingRescueScreenVO> b = buildingService.getRescueScreenB(searchDTO);
		return R.data(b);
	}

	@PostMapping("/elevator")
	@ApiOperationSupport(order = 32)
	@ApiOperation("困人救援高发电梯top10")
	public R<List<ElevatorRescueScreenVO>> byElevator(@RequestBody SugarRescueSearchDTO searchDTO){
		List<ElevatorRescueScreenVO> b = buildingService.getRescueScreenE(searchDTO);
		return R.data(b);
	}

	@PostMapping("/type")
	@ApiOperationSupport(order = 33)
	@ApiOperation("困人救援高发场所列表")
	public R<List<BuildingRescueScreenVO>> byType(@RequestBody SugarRescueSearchDTO searchDTO){
		List<BuildingRescueScreenVO> b = buildingService.getRescueScreenT(searchDTO);
		return R.data(b);
	}

	@GetMapping("/user")
	@ApiOperationSupport(order = 34)
	@ApiOperation("困人救援人员top10")
	public R<List<UserRescueScreenVO>> byUser(Date beginTime,Date endTime,Integer sort){
		List<UserRescueScreenVO> b = buildingService.getRescueScreenU(beginTime,endTime,sort);
		return R.data(b);
	}

	@PostMapping("/resources")
	@ApiOperationSupport(order = 35)
	@ApiOperation("救援统计渠道来源饼图")
	public R<Map<String,String>> byResources(@RequestBody SugarRescueSearchDTO searchDTO){
		Map<String,String> result = rescueService.countByResources(searchDTO);
		return R.data(result);
	}

	/**
	 * 时间参数公共抽取
	 * @param type type
	 * @return time map
	 */
	private Map<String,Date> limitTime(Integer type){
		int t;
		if(Func.isNull(type)){
			t = 1;
		}else{
			t = type;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND,0);
		Date endTime = calendar.getTime();
		Date beginTime;
		if(t == 1){
			// 将日期向前推7天
			calendar.add(Calendar.DATE, -7);
		}else{
			// 将日期向前推30天
			calendar.add(Calendar.DATE, -30);
		}
		beginTime = calendar.getTime();
		Map<String,Date> timeMap = new HashMap<>(2);
		timeMap.put("beginTime",beginTime);
		timeMap.put("endTime",endTime);
		return timeMap;
	}




}
